package com.cib.config;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @描述:RestTemplateConfig指定底层使用httpclient进行调用
 * @Author: zhangh
 * @Time: 2021/9/2 10:43
 **/
@Configuration
public class RestTemplateConfig {
    @Value("${rest.template.requestTimeout}")
    private int requestTimeout;
    @Value("${rest.template.connectTimeout}")
    private int connectTimeout;
    @Value("${rest.template.readTimeout}")
    private int readTimeout;
    @Value("${rest.template.poolSize}")
    private int poolSize;
    @Value("${rest.template.maxConnPerRounte}")
    private int maxConnPerRounte;
    @Value("${rest.template.idlTimeout}")
    private int idlTimeout;


    @Bean
    public HttpClient createHttpClient(){
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setMaxConnPerRoute(maxConnPerRounte);
        httpClientBuilder.setMaxConnTotal(poolSize);
       // 使HttpClient的这个实例使用后台线程主动从连接池中驱逐过期的连接。
        httpClientBuilder.evictExpiredConnections();
        //使此HttpClient实例使用后台线程主动从连接池中逐出空闲连接。
        httpClientBuilder.evictIdleConnections(idlTimeout, TimeUnit.MILLISECONDS);
        return httpClientBuilder.build();
    }

    @Bean
    public ClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient){
        HttpComponentsClientHttpRequestFactory httpRequestFactory= new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(requestTimeout);
        httpRequestFactory.setConnectTimeout(connectTimeout);
        httpRequestFactory.setReadTimeout(readTimeout);
        return httpRequestFactory;
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory){
       // 重新设置StringHttpMessageConverter字符集为UTF-8，解决中文乱码问题
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        HttpMessageConverter<?> converterTarget = null;
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter.getClass()== StringHttpMessageConverter.class){
                converterTarget=messageConverter;
            }

        }
        messageConverters.remove(converterTarget);
        messageConverters.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        return restTemplate;

    }



}
